package com.douguo.recipe.network;

import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.douguo.recipe.CalendarActivity;
import com.douguo.recipe.object.Caipu;
import com.douguo.recipe.object.Calendar;

import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.Xml;

public class GetCalendar {
	private static final String TAG = "GetCalendar";

	public static void getCaipuList(int year, int month, final Handler handler) {
		String yearMonth = String.valueOf(year) + "-" + String.valueOf(month);
		final List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		NameValuePair nvp = new BasicNameValuePair("month", yearMonth);
		nvps.add(nvp);
		new Thread(new Runnable() {

			public void run() {
				DouguoConnection mConn = new DouguoConnection(
						Constant.URL_GET_CALENDAR, nvps, mParseListener,
						DouguoConnection.GET_METHOD);
				List<Calendar> calendars = (List<Calendar>) mConn.Connect();
				handler.sendMessage(Message.obtain(handler,
						CalendarActivity.MSG_CALENDAR, calendars));
			}
		}).start();
	}

	private static OnParseListener mParseListener = new OnParseListener() {

		public Object onParsing(InputStream is) {
			XmlPullParser parser = Xml.newPullParser();
			List<Calendar> calendars = null;
			Calendar calendar = null;
			try {
				parser.setInput(is, null);
				int eventType = parser.getEventType();
				while (eventType != XmlPullParser.END_DOCUMENT) {
					String name = null;
					name = parser.getName();
					switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
						calendars = new ArrayList<Calendar>();
						break;
					case XmlPullParser.START_TAG:
						if (name.equalsIgnoreCase("recipe")) {
							calendar = new Calendar();
							calendar.mRecipeId = parser.getAttributeValue(null,
									"id");
						} else if (name.equalsIgnoreCase("name")) {
							if (calendar != null)
								calendar.mName = parser.nextText();
						} else if (name.equalsIgnoreCase("thumbnail")) {
							if (calendar != null)
								calendar.mPicPath = parser.nextText();
						} else if (name.equalsIgnoreCase("cookday")) {
							String[] date = parser.nextText().split("-");
							if (calendar != null && date.length == 3) {
								calendar.mDay = date[2];
							}

						}
						break;
					case XmlPullParser.END_TAG:
						if (name.equalsIgnoreCase("recipe")) {
							calendars.add(calendar);
						}
						break;
					}
					eventType = parser.next();
				}
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
//				return null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
//				return null;
			}

			return calendars;

		}
	};
}
